var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()

Page({
  data: {
    isShowTextarea: false, //是否显示文本框 
    "rush_id" : 0,  // '抢修ID',
    "ticket_num" : "",  //'抢修单号',
    "company" : "",  //'企业用户',
    "factory" : "",  //'站房名称',
    "equipment" : "",  //
    "leader" : "",  // '抢修负责人',
    "staff" : "",  //'抢修人员',
    "content" : "",  // '抢修内容',
    "source" : "",  //'抢修单来源',
    "dispatch_time" : "",  // '派单时间',
    "STATUS" : "派单中",  //'抢修状态(1:派单中,2:抢修中,3:已完成)',
    "del_flag" : "",  //删除标志（0代表存在 2代表删除）
    "PROCESS" : "",  // '抢修过程',
    "reback" : "",  // '回填情况',
    "image1" : "",  // '图片1',
    "image2" : "",  // '图片2',
    "image3" : "",  // '图片3',
    "defectNumber" : "",  //缺陷单号
    "info":[],  //详细信息  
    "report":[],//报修的详细信息
    "isSet":true,
    "invalidReason":'',//作废原因
    "isLose": false, // 打开作废弹框
    _longitude: 116.3972282409668, //当前地图位置的中心点(默认定位北京)
    _latitude: 39.90960456049752, //当前地图位置的中心点(默认定位北京)
    _scale: 13, //当前地图的缩放系数
    markers:[//地图图标
      {
      id: 0
      ,iconPath: "/assets/map_localtion_red.png"
      ,longitude: 116.3972282409668
      ,latitude: 39.90960456049752
      ,width: 30
      ,height: 30
      }
    ]
  },
  onLoad: function (options) {
    this.setData({
      rush_id: options.rush_id ||0,
      ticket_num: options.ticket_num ,
    })

    this.getInfo()
  },
  //##input 输入##
  input_handle(e) {
    this.setData({
      [e.currentTarget.dataset.key]: e.detail.value
    })
  },
  //##input 输入##
  openLoseHandle() { // 打开作废弹窗
    this.setData({ isLose: true })
  },
  setZuoFei(){//作废
    if($.isNull(this.data.invalidReason)){
      $.toast("作废原因不能为空")
        return
    }
    var params = {
      "ticket_num": this.data.ticket_num,
      "invalidReason":this.data.invalidReason,//作废原因
      "invalidPersonid":wx.getStorageSync('user_id'),//作废人id
      "invalidPerson":wx.getStorageSync('user_name'),//作废人
      "type":4,
    }
    api.$post(api.robManage.updateStatus, params).then((res) => {
      $.toast('作废成功')
      setTimeout(() => {
        $.linkurl('', 'back')
      }, 1500)
    })
  },
  getInfo() { // 获取详情信息
    var params = {
      rush_id: this.data.rush_id,
      ticket_num: this.data.ticket_num ,
    }
    api.$get(api.robManage.info, params).then((res) => { 
      var info=res.data;
      this.setData({
        info: res.data,
        PROCESS:res.data.process|| "",
        _longitude:info.longitude || 116.3972282409668,
        _latitude:info.latitude ||  39.90960456049752,
        address:info.address || '',
        markers:[//地图图标
         {
         id: 0,
         iconPath: "/assets/map_localtion_red.png",
         longitude: info.longitude || 116.3972282409668,
         latitude: info.latitude || 39.90960456049752,
         width: 30,
         height: 30,
         }
       ]
      })

      //查询报修或者的详情
      var params1 = {
        "link_ticket": res.data.defectNumber,
        'defectNumber':res.data.defectNumber,
      }
      api.$get(api.repairsManage.infoByLinkticket, params1).then((res) => {
        this.setData({
          report: res.data
        })
      })
    })


  },

  upPicsHandle(e) { // ##图片组件传出来的数据##
    var { pics, key } = e.detail
    console.log(pics, key)
    this.setData({
      [key]: pics.length>0?pics[0]:[]
    })
   
  },
  toFocusHandle() { //解决textarea placeholder 层级过高的bug 微信好坑 
    this.setData({
      isShowTextarea: true
    })
    setTimeout(() => {
      this.setData({
        isFocus: true
      })
    }, 100)
  },
 async updateStatusHandle(e){ // 更新状态
    var {type}= $.detail(e) 
    if(type==2){//点击到达现场,状态变成抢修中
      var latitude=this.data.info.latitude ||'';
      var longitude=this.data.info.longitude ||'';
      var distance= await app.getDistance(parseFloat(latitude),parseFloat(longitude));
      // var distance= 0.1;
      console.log("距离",distance)
        if(typeof(distance)=='number'){
          if(distance>5 && wx.getStorageSync('user_id')!=1){//距离较远  不允许更新状态
            $.toast('您当前位置距目标位置较远,无法开始计划任务')
            return
          }
        }
    }
    var params = {
      rush_id: this.data.rush_id,
      ticket_num:this.data.ticket_num,
      type
    }
    api.$post(api.robManage.updateStatus, params).then((res) => {
      switch (type){
        case 2: // 抢修中
          this.getInfo()
          $.toast('更新成功')
          setTimeout(()=>{
            $.linkurl('', 'back')
          },500)
          break;
        case 3: // 已完成
          $.toast('更新成功')
          setTimeout(()=>{
            $.linkurl('', 'back')
          },500)
          break;
        case 4: // 已完成
          $.toast('作废成功')
          setTimeout(()=>{
            $.linkurl('', 'back')
          },500)
          break;

      }
      this.setData({
        info: res.data
      })
    })
  },
  update(){
    if(this.data.image1 == '') {
      $.toast('上传主设备图片')
    }else if (this.data.image2=='') {
      $.toast('请上传局部照片')
    } else if (this.data.image3 == '') {
      $.toast('请上传设备铭牌照片')
    } else if (this.data.PROCESS == ''){
        $.toast('请填写抢修过程')
    }else{
      var params = {
        "ticket_num": this.data.ticket_num,
        "PROCESS" : this.data.PROCESS,  // '抢修过程',
        "image1" : this.data.image1,  // '图片1',
        "image2" : this.data.image2,  // '图片2',
        "image3" : this.data.image3,  // '图片3',
        "status": "已完成"
      }
  
        api.$post(api.robManage.update,params).then((res)=>{
          if(res.data!=null){
              $.toast('更新成功')
              setTimeout(()=>{
                $.linkurl('', 'back')
              },1500)
          }else{
              $.toast('更新失败')
          }

        })
    }

  },
  //新增缺陷
  toDefect(){
    var data=this.data.info;
    data.source="抢修";
    $.linkurl(`/pages/defectManage/defectManageFlawRegister/defectManageFlawRegister?info=${JSON.stringify(data)}&type=4`)
  },
  openMapHandle(){ // 打卡地图
    if($.isNull(this.data._latitude) || $.isNull(this.data._longitude)){
      $.toast("对不起,该厂站未提供经纬度信息,无法开启地图服务")
      return
    }
    wx.openLocation({
      latitude: this.data._latitude,
      longitude: this.data._longitude,
      address: this.data.address,
      scale: 18
    })
  },

})